import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
	state: {
		token: uni.getStorageSync('token') ?? '',
		uid: uni.getStorageSync('uid') ?? '',
		realname: uni.getStorageSync('realname') ?? '',
		imageurl: uni.getStorageSync('imageurl') ?? '',
		has_info: uni.getStorageSync('has_info') ?? false,
		mobile: uni.getStorageSync('mobile') ?? '',
	},
	mutations: {
		setToken(state, token) {
			state.token = token;
		},
		setUid(state, uid) {
			state.uid = uid;
		},
		setRealname(state, realname) {
			state.realname = realname;
		},
		setImageurl(state, imageurl) {
			state.imageurl = imageurl;
		},
		setHas_info(state, has_info) {
			state.has_info = has_info;
		},
		setMobile(state, mobile) {
			state.mobile = mobile;
		},
	},
	actions: {
		// 设置token
		setToken({
			commit
		}, token) {
			if (token) {
				uni.setStorageSync('token', token);
			} else {
				uni.clearStorageSync('token');
			}
			commit('setToken', token);
		},
		// 设置uid
		setUid({
			commit
		}, uid) {
			if (uid) {
				uni.setStorageSync('uid', uid);
			} else {
				uni.clearStorageSync('uid');
			}
			commit('setUid', uid);
		},
		// 设置姓名
		setRealname({
			commit
		}, realname) {
			if (realname) {
				uni.setStorageSync('realname', realname);
			} else {
				uni.clearStorageSync('realname');
			}
			commit('setRealname', realname);
		},
		// 设置公司
		setImageurl({
			commit
		}, imageurl) {
			if (imageurl) {
				uni.setStorageSync('imageurl', imageurl);
			} else {
				uni.clearStorageSync('imageurl');
			}
			commit('setImageurl', imageurl);
		},
		// 设置用户类型
		setHas_info({
			commit
		}, has_info) {
			if (has_info) {
				uni.setStorageSync('has_info', has_info);
			} else {
				uni.clearStorageSync('has_info');
			}
			commit('setHas_info', has_info);
		},
		setMobile({
			commit
		}, mobile) {
			if (mobile) {
				uni.setStorageSync('mobile', mobile);
			} else {
				uni.clearStorageSync('mobile');
			}
			commit('setMobile', mobile);
		},
		// 登录
		login({
			dispatch
		}, {
			token,
			uid,
			realname,
			company,
			user_type,
			mobile,
		}) {
			dispatch('setToken', token);
			dispatch('setUid', uid);
			dispatch('setRealname', realname);
			dispatch('setCompany', company);
			dispatch('setHas_info', has_info);
			dispatch('setMobile', mobile);
		},
		// 退出登录
		logout({
			dispatch
		}) {
			dispatch('setToken', '');
			dispatch('setUid', '');
			dispatch('setRealname', '');
			dispatch('setImageurl', '');
			dispatch('setHas_info', '');
			dispatch('setMobile', '');
		},
	},
})
